import ipaddress
import tldextract
# 发送邮件的模块
from django.core.mail import send_mail


def validate_ip(ip):
    try:
        # 将字符串转换为 IPv4Address 或者 IPv6Address 对象
        ipaddress.IPv4Network(ip) if '.' in ip else ipaddress.IPv6Network(ip)

        return True

    except ValueError as e:
        print("Invalid IP Address")
        return False


def is_valid_domain(url):
    extracted = tldextract.extract(url)
    is_valid = all([extracted.domain, extracted.suffix])

    if is_valid:
        print('Valid domain')
    else:
        print('Invalid domain')


def send_email(subject, message, recipient_list):
    send_mail(
        subject,
        message,
        '1210777805@qq.com',  # 发件人
        recipient_list,
        fail_silently=False,
    )


if __name__ == '__main__':
    # 使用函数发送邮件
    send_email('验证码', '5263', ['18152007693@163.com'])
